package com.cy.ajax.controller;

import com.cy.ajax.bean.City;
import com.cy.ajax.bean.Province;
import com.cy.ajax.bean.ResponseResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class MainController {
@RequestMapping("/index.do")
    public String showIndex(){
        return "index";
    }
    @RequestMapping("/get_city.do")

    public ResponseResult<City> getCity() {
        City city = new City(3, "威海");
        ResponseResult<City> rr
                = new ResponseResult<City>(1, null, city);
        return rr;
    }

    @RequestMapping("/get_province.do")

    public String getProvince() {
        List<Province> list = new ArrayList<Province>();
        Province province;

        province = new Province(1, "BeiJing");
        list.add(province);
        province = new Province(2, "ShangHai");
        list.add(province);
        province = new Province(3, "GuangZhou");
        list.add(province);
        province = new Province(4, "ShenZhen");
        list.add(province);
        province = new Province(5, "HangZhou");
        list.add(province);

        return list.toString();
    }

    @RequestMapping("/get_provinces.do")

    public String getProvinces() {
        return "{\"provinces\":[{ \"id\": \"1\", \"name\": \"GuangDong\" },{ \"id\": \"2\", \"name\": \"HeBei\" },{ \"id\": \"3\", \"name\": \"HuBei\" },{ \"id\": \"4\", \"name\": \"ShanDong\" }]}";
    }

    @RequestMapping("/get_cities.do")

    public String getCities(Integer provinceId) {
        String jsonString;
        switch (provinceId) {
            case 1:
                jsonString = "{\"cities\":[{ \"id\": \"11\", \"name\": \"GuangZhou\" },{ \"id\": \"12\", \"name\": \"ShenZhen\" },{ \"id\": \"13\", \"name\": \"ZhuHai\" }]}";
                break;

            case 2:
                jsonString = "{\"cities\":[{ \"id\": \"21\", \"name\": \"ShiJiaZhuang\" },{ \"id\": \"22\", \"name\": \"BaoDing\" },{ \"id\": \"23\", \"name\": \"QinHuangDao\" }]}";
                break;

            case 3:
                jsonString = "{\"cities\":[{ \"id\": \"31\", \"name\": \"WuHan\" },{ \"id\": \"32\", \"name\": \"HuangShi\" },{ \"id\": \"33\", \"name\": \"JinZhou\" }]}";
                break;

            case 4:
                jsonString = "{\"cities\":[{ \"id\": \"41\", \"name\": \"QingDao\" },{ \"id\": \"42\", \"name\": \"YanTai\" },{ \"id\": \"43\", \"name\": \"WeiHai\" }]}";
                break;

            default:
                jsonString = null;
                break;
        }
        return jsonString;
    }

}